package com.sreManager.controller.web;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sreManager.Constant;
import com.sreManager.domain.LoginUsers;
import com.sreManager.mapper.CompanyBuildingMapper;
import com.sreManager.mapper.LogsMapper;
import com.sreManager.web.util.LoginManager;

@Controller
@RequestMapping(value = { "/" })
public class DownloadController extends CommonController {
	@Autowired
	CompanyBuildingMapper buildingMapper;
	
	@Autowired LogsMapper logsMapper;

	// 검색 목록 - 엑셀파일 변환 및 다운로드
	@RequestMapping("companyExcel")
	public String companyExcel(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(required = false) String logMsg)
			throws UnknownHostException {
		
		model.addAttribute("HeaderList", companyHeader());
		model.addAttribute("excelList", buildingMapper.selectBuildingList(paramMap));
		model.addAttribute("target", "업체검색목록");

		// 로그인 유저 정보
		LoginUsers loginUser = LoginManager.getInstance().getSession(request);
		
		String logMenu=Constant.MENU_SEARCH;
        if(!loginUser.getRole().equals(Constant.USER)){
			logMenu = Constant.MENU_COMPANY;
		}
        
		// 로그 
		if(logMsg != null){
			logsService.insertLog(request, Constant.LOG_TYPE_EXCEL, logMenu, logMsg);
		}else{
			logsService.insertLog(request, Constant.LOG_TYPE_EXCEL, logMenu, logMsg);			
		}
		
		return "excelView";
	}
	
	private List<String> companyHeader() {
		List<String> list = new ArrayList<String>();
		list.add("NO");
		list.add("시설명");
		list.add("견적일");
		list.add("측정일");
		list.add("구분");
		list.add("사업자명");
		list.add("구주소");
		list.add("담당자");
		list.add("부서");
		list.add("직위");
		list.add("전화");
		list.add("핸드폰");
		list.add("팩스");
		list.add("메일");
		return list;
	}

	// 검색 목록 - 엑셀파일 변환 및 다운로드
	@RequestMapping("logExcel")
	public String logExcel(Model model,
			@RequestParam HashMap<String, Object> paramMap,
			@RequestParam(required = false) String logMsg)
			throws UnknownHostException {
		
		model.addAttribute("headerList", logHeader());
		model.addAttribute("excelList", logsMapper.selectLogList(paramMap));
		model.addAttribute("target", "로그목록");
        
		// 로그 
		logsService.insertLog(request, Constant.LOG_TYPE_EXCEL, Constant.MENU_LOG, logMsg);			
		
		return "excelView";
	}

	private List<String> logHeader() {
		List<String> list = new ArrayList<String>();
		list.add("NO");
		list.add("메뉴");
		list.add("로그타입");
		list.add("메세지");
		list.add("발생일");
		list.add("발생자ID");
		list.add("발생자");
		list.add("IP");
		return list;
	}
}
